/*
 * @Author: zhousg
 * @Date: 2020-10-30 16:25:17
 * @Last Modified by: zhousg
 * @Last Modified time: 2020-11-02 11:02:15
 */

import XtxMessage from '@/components/xtx-message'
import Vue from 'vue'
const Message = Vue.extend(XtxMessage)
let timer = null
const removeDom = () => {
  window.clearTimeout(timer)
  const domMessage = document.querySelector('.xtx-message')
  domMessage && document.body.removeChild(domMessage)
}
// 消息提示函数
export default (options) => {
  // 1. 删除之前残余dom和定时器
  removeDom()
  // 2. 创建新的实例，并且挂载dom
  const message = new Message({ data: options })
  document.body.appendChild(message.$mount().$el)
  // 3. 打开消息
  message.open()
  timer = window.setTimeout(() => {
    // 4. 稍后销毁实例和dom
    message.$destroy()
    removeDom()
  }, options.duration || 3000)
}
